package com.md.api.common.utils;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SmsInternationUtils {

    public static Logger logger = LoggerFactory.getLogger(SmsUtil.class);
    //产品名称:云通信短信API产品,开发者无需替换
    static final String regionId = "ap-southeast-1";
    //产品域名,开发者无需替换  85260560921
    static final String domain = "dysmsapi.ap-southeast-1.aliyuncs.com";

    static final String accessKeyId = "LTAI5tAzQVi2KMHjQ379fzZg";
    static final String accessKeySecret = "nVwV32fernPMz2YfNvf1NGYW7g3wll";
    public static boolean sendSms(String phone,String msg){
        DefaultProfile profile = DefaultProfile.getProfile("ap-southeast-1", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(domain);
        request.setSysVersion("2018-05-01");
        request.setSysAction("SendMessageToGlobe");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("To", phone);
        request.putQueryParameter("Message", msg);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            JSONObject jsonObject = JSONObject.parseObject(response.getData());
            if (jsonObject.getString("ResponseCode").equals("OK")){
                return true;
            }else{
                logger.error("短信发送失败："+jsonObject.getString("ResponseDescription"));
                return false;
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        SmsInternationUtils.sendSms("08615215260930","205365");
    }
}
